%aim: write a ciphertext in columns
-module(columns).
-export([getMatrix/2]).

getMatrix(M, List) ->
    getMatrix_helper(M, List, 0, []).

getMatrix_helper(M, _List, M, Matrix) ->
    Matrix ;
getMatrix_helper(M, List, Delta, Matrix) ->
    Row = getRow(M, List, Delta),
    getMatrix_helper(M, List, Delta + 1, [Row|Matrix]).
			        
getRow(M, List, Delta) ->
    getRow_helper(M, List, Delta,  0, []).

getRow_helper(M, List, Delta, K, Row) ->
    N = Delta + (M*K + 1), 
    try lists:nth(N, List) of
	E ->
	    getRow_helper(M, List, Delta, K + 1, [E|Row])        
    catch
	%no more elements
	error:function_clause ->
	    Row
    end.
